package com.gitee.alpha.ie.convert.data;

import com.gitee.alpha.ie.ExcelField;
import com.gitee.alpha.ie.RowWrapper;
import com.gitee.alpha.ie.SimpleReadContext;
import com.gitee.alpha.ie.convert.CellDataConverter;
import com.gitee.alpha.ie.convert.CellValueConverter;
import com.gitee.alpha.ie.convert.SimpleCellValueConverter;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellData;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * 单实体类型数据转换<br>
 *
 * @author 冰羽
 * @version 2.2.0
 */
@Slf4j
public class EntityDataConverter implements CellDataConverter {

  private Class<?> entityClass;
  private SimpleReadContext readContext;
  /** 表头所在行 */
  private Integer rowIndex = 0;

  @Getter private CellValueConverter cellValueConverter;

  public EntityDataConverter(Class<?> entityClass, SimpleReadContext readContext) {
    this(entityClass, readContext, new SimpleCellValueConverter());
  }

  public EntityDataConverter(
      Class<?> entityClass, SimpleReadContext readContext, CellValueConverter cellValueConverter) {
    this.entityClass = entityClass;
    this.readContext = readContext;
    this.cellValueConverter = cellValueConverter;
  }

  @Override
  public RowWrapper convert(
      Map<Integer, List<ExcelField>> excelFields,
      Map<Integer, CellData> originHeader,
      Map<Integer, Object> data,
      AnalysisContext context) {
    RowWrapper wrapper = new RowWrapper();
    Integer rowIndex = context.readRowHolder().getRowIndex();
    int cellNotEmptyCount = 0;

    try {
      Object entity = this.entityClass.newInstance();
      // 默认表头一行，所以只需要判断excelFields获取第一个即可。
      List<ExcelField> list = excelFields.get(this.rowIndex);
      if (CollectionUtil.isEmpty(list)) {
        Set<Integer> indexes = excelFields.keySet();
        for (Integer index : indexes) {
          if (CollectionUtil.isNotEmpty(excelFields.get(index))) {
            this.rowIndex = index;
            list = excelFields.get(index);
            break;
          }
        }
      }

      // 设置Entity字段属性的值
      for (ExcelField excelField : list) {
        try {
          Object o = data.get(excelField.getCellIndex());
          // 值转换函数
          Object cellValue = getCellValueConverter().convert(o, excelField.getJavaType());
          if (cellValue != null) {
            BeanUtil.setProperty(entity, excelField.getName(), cellValue);
            cellNotEmptyCount += 1;
          }
        } catch (Exception e) {
          log.warn("convert cell value exception, error: {}", e);
        }
      }

      wrapper.setExcelFields(list);
      wrapper.setRowIndex(rowIndex);
      wrapper.setRowData(entity);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    if (cellNotEmptyCount == 0) {
      wrapper.setRowData(null);
      this.readContext.setStatusBlank(rowIndex, "数据空行");
    }

    return wrapper;
  }
}
